package com.tyqx.hashchainbiz.controller;

import com.tyqx.hashchainbean.module.JwtRequest;
import com.tyqx.hashchainbean.module.TrackUser;
import com.tyqx.hashchainbean.vo.TrackUserVO;
import com.tyqx.hashchainbiz.controller.config.JwtTokenUtil;
import com.tyqx.hashchainbiz.service.HashChainCountService;
import com.tyqx.hashchainbiz.service.TrackUserService;
import com.tyqx.hashchainbiz.service.impl.JwtUserDetailsService;
import hashchainutil.common.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class JwtAuthenticationController {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private JwtUserDetailsService userDetailsService;

    @Resource
    private TrackUserService trackUserService;

    @Resource
    private HashChainCountService hashChainCountService;

    @RequestMapping(value = "/authenticate", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
        if(trackUserService.getUserByCode(authenticationRequest.getUsername())==null){
            return ResponseEntity.ok(DtoUtil.returnFail("用户名没有找到","100001"));
        }else {

            PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
            TrackUser trackUser=trackUserService.getUserByCode(authenticationRequest.getUsername());
            //String password= DigestUtil.hmacSign(authenticationRequest.getPassword());
            if(!passwordEncoder.matches(authenticationRequest.getPassword(),trackUser.getPassword())){
                return ResponseEntity.ok(DtoUtil.returnFail("密码不匹配","100002"));
            }
        }
        TrackUserVO trackUserVO=authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
        //JwtUserDetailsService.
        //userDetailsService.setPassword(authenticationRequest.getPassword());
        final UserDetails userDetails = userDetailsService
                .loadUserByUsername(authenticationRequest.getUsername());
        final String token = jwtTokenUtil.generateToken(userDetails);
        trackUserVO.setToken(token);
        return ResponseEntity.ok(DtoUtil.returnDataSuccess(trackUserVO,"登录成功"));
    }

    private TrackUserVO authenticate(String username, String password) throws Exception {
        TrackUserVO trackUserVO;
        try {
            trackUserVO = trackUserService.login(username, password);
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (DisabledException e) {
            throw new Exception("USER_DISABLED", e);
        } catch (BadCredentialsException e) {
            e.printStackTrace();
            throw new Exception("INVALID_CREDENTIALS", e);
        }
        return trackUserVO;
    }
}
